<?php

namespace App\Http\Controllers\Wechat;

use App\Models\Assessment;
use App\Models\Category;
use App\Models\Child;
use App\Models\Family;
use App\Models\History;
use App\Models\Grade;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class ChildController extends BaseController
{
    //
    public function index(Request $request)
    {
        $user = $this->getUser();

        $query = $request->only(['child_name']);
        $query['user_id'] = $user->id;
        $where = Child::selectQuery($query);
        $list = Child::where($where)->orderBy('id','desc')->paginate(config('site.other.paginate'));
        $list = $list->appends($query);

        return frontendView('child.index', compact('list', 'user', 'query'));
    }

    public function add(Request $request)
    {
        $user = $this->getUser();

        if ($request->isMethod('post')) {
            $this->validate($request, [
                'name' => 'required|max:255',
                'birthday' => 'required|date',
                'sex' => 'required|in:0,1',
                'address' => 'required|max:255',
                'phone' => 'required|string|max:11',
            ], [], [
                'name' => __("Username"),
                'birthday' => __("Birthday"),
                'sex' => __("Sex"),
                'address' => 'required|max:255',
                'phone' => 'required|string|max:11',
            ]);

            $data = $request->only(['name', 'birthday', 'sex', 'address', 'phone']);
            $data['user_id'] = $user->id;
            $data['agency_id'] = $user->agency_id;

            if (Child::create($data)) {
                Family::firstOrCreate(['phone'=>$data['phone']]);
                return redirect()->route('wechat.child.index', ['user_id' => $user->id])->with('success', __('Create success'));
            }

            return back()->with('error', __('Create failed'))->withInput();
        } else {
            return frontendView('child.add', compact('user'));
        }
    }

    public function edit($id, Request $request)
    {
        $child = Child::find($id);
        if (empty($child)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }
        $user = $this->getUser();
        if (!$user->can('allow', $child)) {
            return back()->with('error', __("Sorry, you are not authorized to access this page."));
        }

        if ($request->isMethod("post")) {
            $this->validate($request, [
                'name' => 'required|max:255',
                'birthday' => 'required|date',
                'sex' => 'required|in:0,1',
                'address' => 'required|max:255',
                'phone' => 'required|string|max:11',
            ], [], [
                'name' => __("Username"),
                'birthday' => __("Birthday"),
                'sex' => __("Sex"),
                'address' => 'required|max:255',
                'phone' => 'required|string|max:11',
            ]);
            $data = $request->only(['name', 'birthday', 'sex', 'address', 'phone']);

            if ($child->update($data)) {
                Family::firstOrCreate(['phone'=>$data['phone']]);
                return redirect()->route('wechat.child.index', ['user_id' => $child->user_id])->with('success', __('Edit success'));
            }

            return back()->with('error', __('Edit failed'));
        } else {

            return frontendView('child.edit', compact('child'));
        }
    }

    public function delete($id)
    {
        $child = Child::find($id);

        if (empty($child)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }
        $user = $this->getUser();
        if (!$user->can('allow', $child)) {
            return back()->with('error', __("Sorry, you are not authorized to access this page."));
        }
/*
        $assessment = Assessment::where("child_id", $id)->exists();
        $history = History::where("child_id", $id)->exists();

        if ($assessment || $history){
            return back()->with('error', "学生有评测数据或者训练数据，无法删除");
        }
        */
        if ($child->delete()) {
            return back()->with('success', __('Delete success'));
        }

        return back()->with('error', __('Delete failed'));
    }

    public function view($id)
    {
        $child = Child::find($id);

        if (empty($child)) {
            return back()->with('error', __('The page you requested was not found'))->withInput();
        }

        $user = $this->getUser();
        if (!$user->can('allow', $child)) {
            return back()->with('error', __("Sorry, you are not authorized to access this page."));
        }

        return frontendView('child.view', compact('child'));
    }

}
